<?php

require(__DIR__.'/../../vendor/autoload.php');

require_once(__DIR__.'/../config/config.php');
require_once(__DIR__.'/../utils/add-suffix.php');
require_once(__DIR__.'/../utils/abs-path.php');
require_once(__DIR__.'/../utils/guess-file-type.php');

use Gregwar\Image\Image;

define("SUCCESS"      , 0);
define("UPLOAD_ERROR" , 1);
define("CAN_NOT_MOVE" , 2);
define("NO_FILES"     , 3);

class ImageServer{

  static function emptyResult(){
    return array(
      'ret' => SUCCESS,
      'msg' => '',
      'urls' => array()
    );
  }

  static function uploadImage($seg="image", $con){
    global $config, $defaultResult;

    if(!isset($con))$con = $config;

    $result = ImageServer::emptyResult();

    if(!is_array($_FILES[$seg])){
      $result["ret"] = NO_FILES;
      $result["msg"] = "不包含上传图片";
      return $result;
    }

    $tmpName = $_FILES[$seg]["tmp_name"];
    $name    = $_FILES[$seg]["name"];
    $error   = $_FILES[$seg]["error"];

    ImageServer::_uploadAndManipulate($error, $tmpName, $name, $result, $con);

    return $result;
  }

  static function _uploadAndManipulate($error, $tmpName, $name, &$result, $con){
    global $config;

    if(!isset($con))$con = $config;

    if ($error == 0) {
      $ext               = pathinfo($name, PATHINFO_EXTENSION);
      $targetFileName    = md5_file($tmpName).'.'.$ext;
      $targetPath        = $con["upload_dir"].'/'.$targetFileName;

      if(!move_uploaded_file($tmpName, $targetPath)){

        $result['ret']  = CAN_NOT_MOVE;
        $result['msg']  = 'error in moving ' + $targetPath + '.\n';
        $result['urls'][] = '';

      }else{
        array_push($result['urls'], $targetPath);
        ImageServer::manipulate($targetPath);
      }

    }else{
      $result['ret'] = UPLOAD_ERROR;
      $result['msg'] = 'error in uploading one file:'.$key;
    }

  }

  static function uploadImages($seg="image", $con){
    global $config, $defaultResult;

    if(!isset($con))$con = $config;

    $result = ImageServer::emptyResult();

    if(!is_array($_FILES[$seg]) || !is_array($_FILES[$seg]["error"])){
      $result["ret"] = NO_FILES;
      $result["msg"] = "不包含上传图片";
      return $result;
    }

    foreach ($_FILES[$seg]["error"] as $key => $error) {
      $tmpName = $_FILES[$seg]["tmp_name"][$key];
      $name    = $_FILES[$seg]["name"][$key];

      ImageServer::_uploadAndManipulate($error, $tmpName, $name, $result, $con);
    }

    return $result;
  }

  static function manipulate($file, $con){
    global $config;

    if(!isset($con))$con = $config;

    foreach ($con["transform"] as $t) {
      $targetPath = addSuffix($file, $t["suffix"]);
      $ext        = guessType($file);
      $opendImage = Image::open($file);
      $opendImage->setCacheDir($con["cache_dir"]);
      $opendImage->setFallback($con["fallback"]);

      if($t["crop"]){
        $opendImage->zoomCrop($t["width"], $t["height"]);
      } else {
        $opendImage->cropResize($t["width"], $t["height"]);
      }

      $opendImage->save($targetPath, $type=$ext, $quality=$t["quality"]);
    }
  }
}
/* End of file core.php */
